package com.musifind.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.musifind.activities.R;

public class GeneralHelper {

	private Context con;

	public GeneralHelper(Context con) {
		this.con = con;
	}

	// rounded corner image
	public static Bitmap roundCornerImage(Bitmap src, float round) {
		if (src != null) {
			// Source image size
			int width = src.getWidth();
			int height = src.getHeight();

			// create result bitmap output
			Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
			// set canvas for painting
			Canvas canvas = new Canvas(result);
			canvas.drawARGB(0, 0, 0, 0);

			// configure paint
			final Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(Color.BLACK);

			// configure rectangle for embedding
			final Rect rect = new Rect(0, 0, width, height);
			final RectF rectF = new RectF(rect);

			// draw Round rectangle to canvas
			canvas.drawRoundRect(rectF, round, round, paint);

			// create Xfer mode
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			// draw source image to canvas
			canvas.drawBitmap(src, rect, rect, paint);

			// return final image
			return result;
		}
		return null;
	}

	// parse drawable image to bitmap
	public static Bitmap drawableToBitmap(Drawable drawable) {
		if (drawable instanceof BitmapDrawable) {
			return ((BitmapDrawable) drawable).getBitmap();
		}

		Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
				Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
		drawable.draw(canvas);

		return bitmap;
	}

	// getting the signature of app by debug.keystore
	public void getSignature() {
		try {
			PackageInfo info = con.getPackageManager().getPackageInfo("com.musifind.activities",
					PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				Log.d("getSignature(): MY KEY HASH:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
				// System.out.println("KEY HASH: " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
			}
		} catch (Exception e) {
			Log.e("Package Not Found Exception", "Package not found");
		}
	}

	public static Bitmap getBitmapFromURL(String src) {
		try {
			URL url = new URL(src);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			Bitmap myBitmap = BitmapFactory.decodeStream(input);
			return myBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void showRedToast(Context context, String toastMessage) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View customToastRoot = inflater.inflate(R.layout.toast_red, null);
		TextView txtToast = (TextView) customToastRoot.findViewById(R.id.txtRedToast);
		txtToast.setText(toastMessage);
		Toast customtoast = new Toast(context);

		int offset = Math.round(30 * context.getResources().getDisplayMetrics().density);

		customtoast.setView(customToastRoot);
		customtoast.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, offset);
		customtoast.setDuration(Toast.LENGTH_SHORT);
		customtoast.show();
	}

	public static void showPurpleToast(Context context, String toastMessage) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View customToastRoot = inflater.inflate(R.layout.toast_purple, null);
		TextView txtToast = (TextView) customToastRoot.findViewById(R.id.txtPurpleToast);
		txtToast.setText(toastMessage);
		Toast customtoast = new Toast(context);

		int offset = Math.round(30 * context.getResources().getDisplayMetrics().density);

		customtoast.setView(customToastRoot);
		customtoast.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, offset);
		customtoast.setDuration(Toast.LENGTH_SHORT);
		customtoast.show();
	}

	public static void showBlueToast(Context context, String toastMessage) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View customToastRoot = inflater.inflate(R.layout.toast_blue, null);
		TextView txtToast = (TextView) customToastRoot.findViewById(R.id.txtBlueToast);
		txtToast.setText(toastMessage);
		Toast customtoast = new Toast(context);

		int offset = Math.round(30 * context.getResources().getDisplayMetrics().density);

		customtoast.setView(customToastRoot);
		customtoast.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, offset);
		customtoast.setDuration(Toast.LENGTH_SHORT);
		customtoast.show();
	}

	public static String bitmapToString(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
		byte[] b = baos.toByteArray();
		String temp = Base64.encodeToString(b, Base64.DEFAULT);
		return temp;
	}

	public static Bitmap stringToBitmap(String encodedString) {
		try {
			byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
			Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
			return bitmap;
		} catch (Exception e) {
			e.getMessage();
			return null;
		}
	}

	public static Uri getImageUri(Context inContext, Bitmap inImage) {
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
		String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
		return Uri.parse(path);
	}

}
